// 第一题
// 关于错误捕获到正确姿势

// 1、当try中抛出错误将被catch捕获,且catch中也抛出了错误那么必须在外层中的trycatch中方捕获
try {
    throw new Error('error 1')
} catch (error) {
    console.log(error);
    // 这里抛出错误需要在外层捕获 否则程序将终止
    // throw new Error('error 2');
}
console.log('error1 after');


// 2、finally在try或catch后都必然会执行
try {
    console.log('hello 21')
    throw new Error('error 22')
} catch (error) {
    console.log(error)
} finally {
    console.log('hello 23')
}


function fn() {
    let a = 1;
    try {
        a = 2;
        return a
    } catch (error) {
        
    } finally {
        a = 3;
        return a;
    }
    return a;
}
console.log('a:', fn())


function fn2() {
    let a = 1;
    try {
        a = 2;
        throw new Error(a);
        return a
    } catch (error) {
        a = 4
        return a;
    } finally {
        a = 3;
        // return a;
    }
    return a;
}
console.log('a2:', fn2())

